package com.yf.modules.security.model.dto;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author chentianwei
 * @date 2023/5/4 15:04
 * @description 重新定制UserDetails
 */
@Slf4j
@Data
public class JwtUserDto implements UserDetails {

    private final UserDto user;
    private final List<Long> dataScopes;
    private final List<AuthorityDto> authorities;


    public Set<String> getRoles() {
        return Objects.isNull(authorities) ? null : authorities.stream().map(AuthorityDto::getAuthority).collect(Collectors.toSet());

    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return user.getEnabled();
    }

    public UserDto getUser() {
        return user;
    }

    public List<Long> getDataScopes() {
        return dataScopes;
    }

    @Override
    public List<AuthorityDto> getAuthorities() {
        return authorities;
    }

    public JwtUserDto(UserDto user, List<Long> dataScopes, List<AuthorityDto> authorities) {
        this.user = user;
        this.dataScopes = dataScopes;
        this.authorities = authorities;
    }
}
